package de.hwrberlin.remo.diagram.custom.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.part.FileEditorInput;

import de.hwrberlin.remo.diagram.part.RemoDiagramEditor;
import de.hwrberlin.remo.model.KnowledgeBase;

public abstract class AbstractRemoDiagramAction implements IEditorActionDelegate {

	protected RemoDiagramEditor expertSystemEditor;
	protected KnowledgeBase knowledgeBase;
	protected IFile modelFile;
	protected String modelFilePath;
	
	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		
		if(targetEditor instanceof RemoDiagramEditor) {
			expertSystemEditor = (RemoDiagramEditor) targetEditor;
			
			// Get expert system element from domain
			TransactionalEditingDomain editingDomain = expertSystemEditor.getEditingDomain();
			EList<Resource> resources = editingDomain.getResourceSet().getResources();
			EObject contents = resources.get(0).getContents().get(0);;
			
			if(contents instanceof KnowledgeBase) {
				knowledgeBase = (KnowledgeBase) contents;
			}
			
			// Get model file
			IEditorInput editorInput = expertSystemEditor.getEditorInput();
			if (editorInput instanceof FileEditorInput) {
				FileEditorInput fileInput = (FileEditorInput) editorInput;
				modelFile = fileInput.getFile();
			
			} else if(editorInput instanceof URIEditorInput) {
				URI uri = ((URIEditorInput) editorInput).getURI();
				modelFilePath = uri.toFileString();
			}
		}
	}
}